package com.resou.reader.mine.mybooklist;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.resou.reader.R;
import com.resou.reader.api.entry.MyBookListBean;
import com.resou.reader.base.ResouBaseActivity;
import com.resou.reader.discovery.bookdetail.BookListActivity;
import com.resou.reader.discovery.choicenes.ChoiceneBookActivity;
import com.resou.reader.discovery.choicenes.adapter.ChoiceneBookAdapter;
import com.resou.reader.mine.adapter.MyBooklistBookAdapter;
import com.resou.reader.utils.NetworkUtils;
import com.resou.reader.utils.ToastUtil;
import com.resou.reader.utils.ViewHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyBookListActivity extends ResouBaseActivity<MyBooklistPresenter> implements IMyBooklistContract.IMyBooklistView,
        OnRefreshLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemChildLongClickListener {

    /**
     * 启动我的书单页面
     *
     * @param context
     */
    public static void start(Context context) {
        Intent starter = new Intent(context, MyBookListActivity.class);
        context.startActivity(starter);
        if (context instanceof Activity) {
            ((Activity) context).overridePendingTransition(R.anim.anim_detail_enter, R.anim.anim_no);
        }
    }


    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mSmartRefreshLayout)
    SmartRefreshLayout mSmartRefreshLayout;
    MyBooklistBookAdapter mChoiceneBookAdapter;
    TextView tvConfirm;
    int page = 0;
    View nullView = null;

    AlertDialog alertDialog;
    AlertDialog.Builder builder;

    String bookId = "";

    int pos = 0;

    @Override
    protected void initStatusBar() {

    }

    @Override
    protected void initPresenter() {
        mPresenter = new MyBooklistPresenter(this);
    }

    @Override
    protected void initView() {
        tvTitle.setText("我的书单");
        initDialog();
        mSmartRefreshLayout.setOnRefreshLoadMoreListener(this);
        List<MyBookListBean.UserBookListBean> list = new ArrayList<>();
        mChoiceneBookAdapter = new MyBooklistBookAdapter(R.layout.adapterchoicenebook_list, list);
        mChoiceneBookAdapter.setOnItemChildClickListener(this);
        mChoiceneBookAdapter.setOnItemChildLongClickListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mChoiceneBookAdapter);

        nullView = LayoutInflater.from(this).inflate(R.layout.mybookisnull, null);
        TextView tvGoChoiceneBook = nullView.findViewById(R.id.tvGoChoiceneBook);
        tvGoChoiceneBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChoiceneBookActivity.start(MyBookListActivity.this);
            }
        });

        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetworkUtils.isNetworkConnected(MyBookListActivity.this)) {
                    mPresenter.deleteBookList(bookId);
                    alertDialog.dismiss();
                } else {
                    ToastUtil.makeShortToast("网络异常");
                }
            }
        });

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_my_book_list;
    }

    @Override
    public void retryRefreshData() {
        page = 0;
        mChoiceneBookAdapter.getData().clear();
        mPresenter.quearBookList(page);
    }

    @Override
    public void initData() {

    }

    @Override
    public void showContent() {
        dissLoading();
    }

    @Override
    public void showProgress() {
        showLoading();
    }

    @Override
    protected void onResume() {
        super.onResume();
        page = 0;
        mChoiceneBookAdapter.getData().clear();
        mPresenter.quearBookList(page);
    }

    @Override
    public void showError(Throwable throwable) {
        ToastUtil.makeShortToast("网络异常");
        dissLoading();
        showNetErrorView(R.id.rlContent);
    }

    @Override
    public void showEmpty() {
        if (null == nullView) {
            nullView = LayoutInflater.from(this).inflate(R.layout.mybookisnull, null);
        }
        mChoiceneBookAdapter.setEmptyView(nullView);
    }


    @Override
    public void showBookList(List<MyBookListBean.UserBookListBean> listBeans) {
        if (page == 0) {
            mSmartRefreshLayout.finishRefresh();
            mChoiceneBookAdapter.setNewData(listBeans);
        } else {
            mChoiceneBookAdapter.addData(listBeans);
        }
    }

    @Override
    public SmartRefreshLayout getSmartRefreshLayout() {
        return mSmartRefreshLayout;
    }

    @Override
    public void cancellCollection() {
        if (mChoiceneBookAdapter.getData().size() != 0) {
            mChoiceneBookAdapter.remove(pos);
            if (mChoiceneBookAdapter.getData() != null && mChoiceneBookAdapter.getData().size() == 0) {
                showEmpty();
            }
        } else {
            showEmpty();
        }
    }

    @Override
    public void showInfo(String info) {

    }


    @OnClick(R.id.ivBack)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page++;
        mPresenter.quearBookList(page);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 0;
        mChoiceneBookAdapter.getData().clear();
        mPresenter.quearBookList(page);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        MyBookListBean.UserBookListBean userBookListBean = (MyBookListBean.UserBookListBean) adapter.getData().get(position);
        String[] url = userBookListBean.getBookListUrl().split(",");
        BookListActivity.start(MyBookListActivity.this, userBookListBean.getBookListId(), url[0] != null ? url[0] : "");
    }


    private void initDialog() {
        View view = ViewHelper.createView(this, R.layout.dialog_deletebooklist);
        builder = new AlertDialog.Builder(this, R.style.DialogStyle);
        builder.setView(view);
        alertDialog = builder.create();
        alertDialog.setCanceledOnTouchOutside(true);
        TextView tvCancell = view.findViewById(R.id.tvCancell);
        tvCancell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != alertDialog) {
                    alertDialog.dismiss();
                }
            }
        });
        tvConfirm = view.findViewById(R.id.tvConfirm);
    }

    @Override
    public boolean onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) {

        pos = position;
        MyBookListBean.UserBookListBean userBookListBean = (MyBookListBean.UserBookListBean) adapter.getData().get(position);
        bookId = userBookListBean.getBookListId();
        alertDialog.show();
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != alertDialog) {
            alertDialog.dismiss();
        }
    }
}
